#include "StdAfx.h"
#include ".\mikedrumtrackbuilder.h"

#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>

MikeDrumTrackBuilder::MikeDrumTrackBuilder(void):m_pMouseHandlerNext(NULL),
												m_pRayPicker(NULL),m_nSelectedRect(-1)
{
	
	mdsVector<3, double> p3Xlate;
	p3Xlate[0] = -30;

	CollisionRect testRect(10.0, 40.0, 90.0);
	testRect.TranslateBy(p3Xlate);
	m_arrRectsCopyFrom[0] = testRect;
	
	
	p3Xlate[1] = 120;
	
	CollisionRect testRect2(10.0, 40.0, 120.0);
	testRect2.TranslateBy(p3Xlate);
	m_arrRectsCopyFrom[1] = testRect2;
}

MikeDrumTrackBuilder::~MikeDrumTrackBuilder(void)
{
}


void MikeDrumTrackBuilder::Draw()
{
	glMatrixMode(GL_MODELVIEW);

	for(int i = 0; i < MaxRectsToCopy(); ++i)
	{
		glPushMatrix();

		glColor3d(0.0, 1.0, 0.0);
		
		double arrRotMatVals[16];

		matrixGlobals::glMatFromM3AndVec<double>(arrRotMatVals,
			m_arrRectsCopyFrom[i].GetOrient(), m_arrRectsCopyFrom[i].GetCenter());

		::glMultMatrixd(arrRotMatVals);

		mdsVector<3, double> vecSize = m_arrRectsCopyFrom[i].GetSize();
		
		glNormal3d(0.0, 0.0, 1.0);
		glBegin(GL_POLYGON);
		glVertex3d(vecSize[0], vecSize[1], vecSize[2]);
		glVertex3d(-vecSize[0], vecSize[1], vecSize[2]);
		glVertex3d(-vecSize[0], -vecSize[1], vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], vecSize[2]);
		glEnd();

		glNormal3d(0.0, 0.0, -1.0);
		glBegin(GL_POLYGON);
		glVertex3d(-vecSize[0], vecSize[1], -vecSize[2]);
		glVertex3d(vecSize[0], vecSize[1], -vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], -vecSize[2]);
		glVertex3d(-vecSize[0], -vecSize[1], -vecSize[2]);
		glEnd();

		glNormal3d(0.0, 1.0, 0.0);
		glBegin(GL_POLYGON);
		glVertex3d(vecSize[0], vecSize[1], -vecSize[2]);
		glVertex3d(-vecSize[0], vecSize[1], -vecSize[2]);
		glVertex3d(-vecSize[0], vecSize[1], vecSize[2]);
		glVertex3d(vecSize[0], vecSize[1], vecSize[2]);
		glEnd();

		glNormal3d(0.0, -1.0, 0.0);
		glBegin(GL_POLYGON);
		glVertex3d(-vecSize[0], -vecSize[1], -vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], -vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], vecSize[2]);
		glVertex3d(-vecSize[0], -vecSize[1], vecSize[2]);
		glEnd();

		glNormal3d(1.0, 0.0, 0.0);
		glBegin(GL_POLYGON);
		glVertex3d(vecSize[0], vecSize[1], vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], -vecSize[2]);
		glVertex3d(vecSize[0], vecSize[1], -vecSize[2]);
		glEnd();

		glNormal3d(1.0, 0.0, 0.0);
		glBegin(GL_POLYGON);
		glVertex3d(-vecSize[0], vecSize[1], -vecSize[2]);
		glVertex3d(-vecSize[0], -vecSize[1], -vecSize[2]);
		glVertex3d(-vecSize[0], -vecSize[1], vecSize[2]);
		glVertex3d(-vecSize[0], vecSize[1], vecSize[2]);
		glEnd();

		glPopMatrix();
	}

	int nWhichIter = 0;
	for(std::vector<CollisionRect>::iterator iter = m_vecRects.begin();
		iter != m_vecRects.end(); ++iter)
	{
		glPushMatrix();
		
		double arrRotMatVals[16];

		if(nWhichIter == m_nSelectedRect)
		{
			glColor3d(1.0, 0.0, 0.0);
		}
		else
		{
			glColor3d(0.7, 0.7, 0.0);
		}

		matrixGlobals::glMatFromM3AndVec<double>(arrRotMatVals,
			(*iter).GetOrient(), (*iter).GetCenter());

		::glMultMatrixd(arrRotMatVals);

		//::glTranslated((*iter).GetCenter()[0], 
		//	(*iter).GetCenter()[1], 
		//	(*iter).GetCenter()[2]);

		mdsVector<3, double> vecSize = (*iter).GetSize();
		
		glNormal3d(0.0, 0.0, 1.0);
		glBegin(GL_POLYGON);
		glVertex3d(vecSize[0], vecSize[1], vecSize[2]);
		glVertex3d(-vecSize[0], vecSize[1], vecSize[2]);
		glVertex3d(-vecSize[0], -vecSize[1], vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], vecSize[2]);
		glEnd();

		glNormal3d(0.0, 0.0, -1.0);
		glBegin(GL_POLYGON);
		glVertex3d(-vecSize[0], vecSize[1], -vecSize[2]);
		glVertex3d(vecSize[0], vecSize[1], -vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], -vecSize[2]);
		glVertex3d(-vecSize[0], -vecSize[1], -vecSize[2]);
		glEnd();

		glNormal3d(0.0, 1.0, 0.0);
		glBegin(GL_POLYGON);
		glVertex3d(vecSize[0], vecSize[1], -vecSize[2]);
		glVertex3d(-vecSize[0], vecSize[1], -vecSize[2]);
		glVertex3d(-vecSize[0], vecSize[1], vecSize[2]);
		glVertex3d(vecSize[0], vecSize[1], vecSize[2]);
		glEnd();

		glNormal3d(0.0, -1.0, 0.0);
		glBegin(GL_POLYGON);
		glVertex3d(-vecSize[0], -vecSize[1], -vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], -vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], vecSize[2]);
		glVertex3d(-vecSize[0], -vecSize[1], vecSize[2]);
		glEnd();

		glNormal3d(1.0, 0.0, 0.0);
		glBegin(GL_POLYGON);
		glVertex3d(vecSize[0], vecSize[1], vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], vecSize[2]);
		glVertex3d(vecSize[0], -vecSize[1], -vecSize[2]);
		glVertex3d(vecSize[0], vecSize[1], -vecSize[2]);
		glEnd();

		glNormal3d(1.0, 0.0, 0.0);
		glBegin(GL_POLYGON);
		glVertex3d(-vecSize[0], vecSize[1], -vecSize[2]);
		glVertex3d(-vecSize[0], -vecSize[1], -vecSize[2]);
		glVertex3d(-vecSize[0], -vecSize[1], vecSize[2]);
		glVertex3d(-vecSize[0], vecSize[1], vecSize[2]);
		glEnd();

		glPopMatrix();

		++nWhichIter;
	}
	
}

bool MikeDrumTrackBuilder::OnMouseMove(UINT nFlags, double lfX, double lfY )
{
	mdsVector<2, double> p2ScreenCoord; 
	p2ScreenCoord[0] = 1.0-2.0*lfX; p2ScreenCoord[1] = 2.0*lfY-1.0; 
	if(m_pRayPicker)
	{
		mdsRay<double> rayNew;
		m_pRayPicker->PickRayFromScreenCoordStateless(p2ScreenCoord, rayNew);
		
		if(nFlags & MK_LBUTTON)
		{
			if(m_nSelectedRect >= 0)
			{
				mdsVector<3, double> vecCurrPt;
				mdsVector<3, double> vecLastPt;
				if(rayNew.RayHitPlane(0, 0, vecCurrPt) &&
					m_rayOld.RayHitPlane(0, 0, vecLastPt))
				{
					mdsVector<3, double> vecMove(vecCurrPt-vecLastPt);
					m_vecRects[m_nSelectedRect].TranslateBy(vecMove);
				}
			}
			else if(!(m_nFlagsLast & MK_LBUTTON))
			{
				mdsVector<3, double> vecSelectPos;

				int nWhichIter = 0;
				for(std::vector<CollisionRect>::iterator iter = m_vecRects.begin();
					m_nSelectedRect < 0 && iter != m_vecRects.end(); ++iter)
				{
					
					if((*iter).CollideRay(rayNew, vecSelectPos))
					{
						m_nSelectedRect = nWhichIter;
					}

					++nWhichIter;
				}

				if(m_nSelectedRect < 0)
				{
					mdsVector<3, double> vecBump;
					vecBump[0] = -1.0*m_arrRectsCopyFrom[0].GetCenter()[0];

					for(int i = 0; i < MaxRectsToCopy(); ++i)
					{
						if(m_arrRectsCopyFrom[i].CollideRay(rayNew, vecSelectPos))
						{
							m_nSelectedRect = m_vecRects.size();
							m_vecRects.push_back(m_arrRectsCopyFrom[i]);
							m_vecRects[m_nSelectedRect].TranslateBy(vecBump);
						}
					}
				}
			}
			if(m_nSelectedRect >= 0)
			{
				m_rayOld = rayNew;
				m_nFlagsLast = nFlags;
				return true;
			}
		}
		else
		{
			m_nSelectedRect = -1;
		}

		m_rayOld = rayNew;
		m_nFlagsLast = nFlags;
	}

	m_nFlagsLast = nFlags;
	// if we don't need the mouse event
	if(m_pMouseHandlerNext)
	{
		return m_pMouseHandlerNext->OnMouseMove(nFlags, lfX, lfY);
	}
	return false;
}
